<template>
  <view class="video-footer">
    <view class="title">是否观看广告解锁</view>
    <view class="btnList">
      <view class="btn" @click="adClose">否</view>
      <view class="btn" @click="adOpen">是</view>
    </view>
  </view>
</template>

<script>

import {Api} from "../../service/api";

const { getPlayletManager } = tt;
export default {
  data() {
    return {
      pm: null,
      rewardedVideoAd: null, // 广告
      flag: true,
    };
  },
  props: {
    howMuch: {
      type: Number,
      default: 0,
    },
    series: {
      type: Number,
      default: 0,
    }
  },
  async beforeCreate() {
    this.pm = await getPlayletManager({
      is: 'charge' // 和插槽同名
    });
  },
  async created() {
    console.log(this)
    //广告
    //#ifdef MP-TOUTIAO
    // 创建激励广告实例
    this.rewardedVideoAd = tt.createRewardedVideoAd({
      adUnitId: 'ivsxdrf7272o9rjo2l'
    });
    //#endif
    // 监听广告加载事件
    this.rewardedVideoAd.onLoad(() => {
      console.log('激励广告加载成功');
    });
    // 监听广告关闭事件
    this.rewardedVideoAd.onClose(async res => {
      if (res.isEnded && this.flag) {
        // 在这里处理奖励逻辑
        this.flag = false
        const tvInfo = uni.getStorageSync('tvInfo')
        let adNum = uni.getStorageSync('adNum') || 0
        if(tvInfo.adNum){
          adNum += 1
          uni.setStorageSync('adNum',adNum)
        }
        const res = await Api.paySeriesMember({
          tvId: tvInfo.tvId,
          tvName: tvInfo.tvName,
          series: tvInfo.series,
          change_num: 0,
          putInId: tvInfo.putInId,
          type: 2, //1金币  2广告
          back: tvInfo.adNum ? adNum % tvInfo.adNum === 0 : false
        })
        this.payThis()
      } else {
        // this.handle()
        console.log('用户提前关闭了广告');
      }
    });
    // 监听广告错误事件
    this.rewardedVideoAd.onError((err) => {
      console.error('激励广告加载失败：', err);
    });
  },
  methods: {
    adOpen(){
      // 显示激励广告
      this.rewardedVideoAd.show().catch(() => {
        // 如果广告未加载成功，重新加载广告
        this.rewardedVideoAd.load().then(() => {
          this.rewardedVideoAd.show();
        }).catch(err => {
          console.error('激励广告显示失败：', err);
          uni.showToast({
            title: '暂无广告可显示',
            icon: 'none',
          });
        });
      });
    },
    adClose(){
      this.pm.toggleCustomDialog();
    },
    payThis: function (e) {
      console.log("支付解锁当前集", e);
      this.pm.toggleCustomDialog();
      this.pm.setCurrentUnlock();
    },
    payAll: function (e) {
      console.log("支付解锁全集", e);
      this.pm.toggleCustomDialog();
      // this.pm.setCatalog();
    },
    adThis: function (e) {
      console.log("广告解锁当前集", e);
      // 监听视频播放完成
    },
    adAll: function (e) {
      console.log("广告解锁全集", e);
    },
  },
};
</script>

<style>
.video-footer {
  background-color: #212121;
  height: 110rpx;
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding: 60rpx 0 0;
  font-size: 28rpx;
  color: #ffffff;
  border-top-left-radius: 30rpx;
  border-top-right-radius: 30rpx;

  .title{
    margin-left: 45rpx;
  }

  .btnList{
    display: flex;
    gap: 20rpx;
    margin-right: 45rpx;

    .btn{
      border: 2rpx solid #fff;
      padding: 10rpx 35rpx;
      border-radius: 35rpx;
      height: 40rpx;
    }
  }

}
</style>
